package com.xxxx.spring.day13;

import org.aspectj.lang.annotation.Before;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectInstanceFactory;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.aspectj.AspectJMethodBeforeAdvice;
import org.springframework.aop.aspectj.SingletonAspectInstanceFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class D13_2 {

    static class Aspect{
        @Before("execution(* foo())")
        public void before1(){
            System.out.println("before1");
        }

        @Before("execution(* foo())")
        public void before2(){
            System.out.println("before2");
        }
    }

    static class Target{
        public void foo(){
            System.out.println("target foo");
        }
    }

    public static void main(String[] args) {

        AspectInstanceFactory factory = new SingletonAspectInstanceFactory(new Aspect());
        ArrayList<Advisor> list = new ArrayList<>();
        // 高级切面转低级切面类
        for (Method method : Aspect.class.getDeclaredMethods()) {
            if(method.isAnnotationPresent(Before.class)){
                // 解析切点
                String expression = method.getAnnotation(Before.class).value();
                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
                pointcut.setExpression(expression);
                // 通知类
                AspectJMethodBeforeAdvice advice = new AspectJMethodBeforeAdvice(method,pointcut,factory);
                // 切面
                Advisor advisor = new DefaultPointcutAdvisor(pointcut,advice);
                list.add(advisor);
            }
        }
        for (Advisor advisor : list) {
            System.out.println(advisor);
        }
        /**
         *  @Before 前置通知会被转换为下面原始的 AspectJMethodBeforeAdvice 形式, 该对象包含了如下信息
         *      a.通知代码从哪里来
         *      b.切点是什么(这里为啥要切点,后面解释)
         *      c.通知对象如何创建,本例共用同一个 Aspect 对象
         *  类似的通知还有
         *      1.AspectJAroundAdvice (环绕通知)
         *      2.AspectJAfterReturningAdvice
         *      3.AspectJAfterThrowingAdvice
         *      4.AspectJAfterAdvice (环绕通知)
         */
    }
}
